<?php

class Paypal {
	
	const API_USERNAME = 'seller_1251906967_biz_api1.yahoo.com';
	const API_PASSWORD = '1251906977';
	const API_SIGNATURE = 'A88TN9A.3Nw4l-eNKxtTuGmwXzh6AAx5x.5WKQxU7l-9OdEt3OIQ6Px3 ';
	const API_ENDPOINT = 'https://api-3t.sandbox.paypal.com/nvp';
	const PAYPAL_URL = 'https://www.sandbox.paypal.com/webscr&cmd=_express-checkout&token=';
	const VERSION = '58.0';
	
	private function encode( &$val ) {
		$val = urlencode( $val );
	}
	
	function call(array $additionalParams = null) {
		if( !isset( $_SESSION ) ) session_start();
		if( $additionalParams != null ) $r = $additionalParams;
		$r['VERSION'] = (self::VERSION);
		$r['USER'] = (self::API_USERNAME);
		$r['PWD'] = (self::API_PASSWORD);
		$r['SIGNATURE'] = (self::API_SIGNATURE);
		$r['NOSHIPPING'] = '1';

		//array_walk(&$r, array($this,'encode') );
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POST, 1);
		
		$nvpReq = http_build_query( $r );
		curl_setopt($ch,CURLOPT_POSTFIELDS, $nvpReq);

		$response = curl_exec($ch);

		//convrting NVPResponse to an Associative Array
		parse_str($response, &$nvpResArray );
		parse_str($nvpReq, &$nvpReqArray);
		
		if (curl_errno($ch)) {
			// moving to display page to display curl errors
			  $_SESSION['curl_error_no']=curl_errno($ch) ;
			  $_SESSION['curl_error_msg']=curl_error($ch);
			  $location = "APIError.php";
			  header("Location: $location");
		 } else {
			 //closing the curl
				curl_close($ch);
		  }

		return array( $nvpReqArray, $nvpResArray ); // list( request, response );
	
	}

	function getDetails() {
		$r['METHOD'] = 'GetExpressCheckoutDetails';
		$r['VERSION'] = (self::VERSION);
		$r['USER'] = (self::API_USERNAME);
		$r['PWD'] = (self::API_PASSWORD);
		$r['SIGNATURE'] = (self::API_SIGNATURE);
		
		$r['RETURNURL'] = ('http://airborneb/order.php');
		$r['CANCELURL'] =("http://airborneb/index.php" );
		$r['AMT'] = 20;
		
		array_walk(&$r, array($this,'encode') );
		
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, self::API_ENDPOINT);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		curl_setopt($ch, CURLOPT_POST, 1);
		
		$nvpReqArray = http_build_query( $r );
		
		curl_setopt($ch,CURLOPT_POSTFIELDS, $nvpReqArray);

		$response = curl_exec($ch);

		//convrting NVPResponse to an Associative Array
		parse_str($response, &$nvpResArray );
		parse_str($nvpreq, &$nvpReqArray);
		
		if( !isset( $_SESSION ) ) session_start();
		$_SESSION['nvpReqArray']=$nvpReqArray;

		if (curl_errno($ch)) {
			// moving to display page to display curl errors
			  $_SESSION['curl_error_no']=curl_errno($ch) ;
			  $_SESSION['curl_error_msg']=curl_error($ch);
			  $location = "APIError.php";
			  header("Location: $location");
		 } else {
			 //closing the curl
				curl_close($ch);
		  }

	return $nvpResArray;

	}
	
}